package com.alex.controller.admin;

import com.alex.pojo.Admin;
import com.sun.deploy.net.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class MainAdminController {

    private static final String HOME="/admin/home";
    private static final String REDIRECT_HOME="redirect:/admin/home";

    @RequestMapping("/")
    public String index() {
        return "admin/login";
    }

    @RequestMapping("/home")
    public String home() {
        return "admin/home";
    }

    @RequestMapping("/login")
    public String goLogin(HttpSession session) {
        if (session.getAttribute("adminLoginInfo") != null) {
            return "redirect:/admin/home";
        }
        return "admin/login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(HttpServletRequest request, HttpSession session, Model model, String username, String password) {
//        HttpSession session1 = request.getSession();
//        System.out.println(session == session1);
        if (session.getAttribute("adminLoginInfo") != null) {
            return "redirect:/admin/home";
        }
        if ("alex".equals(username) && "123".equals(password)) {
            Admin admin = new Admin("alex", "123");
            session.setAttribute("adminLoginInfo", admin);
            return "redirect:/admin/home";
        }

        model.addAttribute("message", "用户名或密码错误");
        return "/admin/login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("adminLoginInfo");
        return "redirect:/admin/login";
    }
}
